/*
 SGEndGameViewController.m
 Date Created: March 26, 2010
 
 Authors: Frank Lau <flaua@sfu.ca> 
          Karanvir Gill <viper1029@gmail.com>

 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 
 */

#import "SGEndGameViewController.h"
#import "SGGameProgressViewController.h"
#import "SGSortingGameLevel.h"

@implementation SGEndGameViewController

- (SGEndGameViewController*)initWithGame:(SGSortingGame *)newGame {
  self = [super init];
  
  if( self ){
		NSLog(@"Initialize summary view controller");

    sortingGame=[newGame retain];
  }
  return self;
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];
	[self setTitle:@"End Game"];
	
  SGSortingGameLevel *lastLevel=sortingGame.lastLevel;
  currentLevelLabel.text = [NSString stringWithFormat:@"%i", lastLevel.level];
  levelAccuracyLabel.text = [NSString stringWithFormat:@"%.2f%@",lastLevel.accuracy, @"%"];
  levelScoreLabel.text  = [NSString stringWithFormat:@"%i", lastLevel.score];
  
  totalProcessedLabel.text = [NSString stringWithFormat:@"%i", sortingGame.numItemsProcessed];
  totalCorrectlySortedLabel.text = [NSString stringWithFormat:@"%i", sortingGame.numItemsCorrectlySorted];  
  totalScoreLabel.text = [NSString stringWithFormat:@"%i", sortingGame.score];  
  gameAccuracyLabel.text = [NSString stringWithFormat:@"%.2f%@",sortingGame.accuracy, @"%"];
}

- (IBAction)goToMainMenu:(id)sender{	
	[[self navigationController] popToRootViewControllerAnimated:YES];
}

- (IBAction)viewProgressChart:(id)sender {
  NSSet *allLevels=sortingGame.levels;
	NSMutableArray *xValuesForProgressChart=[NSMutableArray arrayWithCapacity:allLevels.count];
  NSMutableArray *yValuesForProgressChart=[NSMutableArray arrayWithCapacity:allLevels.count];
  for(SGSortingGameLevel *oldLevel in allLevels) {
    // Ensure that the levels are sorted in increasing order
    // Use insertion sort
    NSUInteger insertID=xValuesForProgressChart.count;
    for(int i=0;i<xValuesForProgressChart.count;i++) {
      NSUInteger iValue=[[xValuesForProgressChart objectAtIndex:i] unsignedIntValue];
      if(iValue>oldLevel.level) {
        insertID=i;
        break;
      }
    }
    
    [xValuesForProgressChart insertObject:[NSNumber numberWithUnsignedInt:oldLevel.level] atIndex:insertID];
    [yValuesForProgressChart insertObject:[NSNumber numberWithFloat:oldLevel.accuracy] atIndex:insertID];
  }
  
  SGGameProgressViewController *gameProgressViewController=[[SGGameProgressViewController alloc]
                                                            initWithXValues:xValuesForProgressChart
                                                            initWithYValues:yValuesForProgressChart];
	[[self navigationController] pushViewController:gameProgressViewController animated:YES];
	[gameProgressViewController release];	
  
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  [[self navigationController] setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  [sortingGame.managedObjectContext deleteObject:sortingGame]; 
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
	[super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
}


- (void)dealloc {
	[super dealloc];
}


@end